<?php
/**
* curl请求类
*/
class Curl_model extends CI_Model
{
	
	function __construct()
	{
		parent::__construct();
	}
	/**
	 * http get请求
	 * @param  [type]  $url    [请求地址]
	 * @param  integer $header [http请求头]
	 * @return [type]          [description]
	 */
	function _get($url, $header = 0){
		//初始化一个curl会话
		$curl = curl_init();
		//设置请求地址
		curl_setopt($curl, CURLOPT_URL, $url);
		//获取返回值
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//设置代理 本地不设置会报错
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		//设置http请求头
		if(is_array($header)){
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}
		//运行curl
		$result = curl_exec($curl);
		//资源关闭
		curl_close($curl);

		return json_decode($result,true);
	}
	/**
	 * http post请求
	 * @param  [type]  $url    [description]
	 * @param  [type]  $data   [description]
	 * @param  integer $header [description]
	 * @return [type]          [description]
	 */
	function _post($url, $data, $header = 0){
		//初始化curl会话
		$curl = curl_init();
		//设置请求地址
		curl_setopt($curl, CURLOPT_URL, $url);
		//设置返回
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//设置post请求
		curl_setopt($curl, CURLOPT_POST, 1);
		//设置post数据
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		//设置代理 本地不设置会报错
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		//设置http请求头
		if(is_array($header)){
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}
		//运行curl
		$result = curl_exec($curl);
		//资源关闭
		curl_close($curl);

		return json_decode($result,true);

	}
}